package com.scau.XFourPOS.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.scau.XFourPOS.model.Product;
import com.scau.XFourPOS.model.SaleOrder;
import com.scau.XFourPOS.model.SaleOrderItem;
import com.scau.XFourPOS.model.SaleReturn;
import com.scau.XFourPOS.model.SaleReturnItem;
import com.scau.XFourPOS.service.ProductService;
import com.scau.XFourPOS.service.SaleOrderItemService;
import com.scau.XFourPOS.service.SaleOrderService;
import com.scau.XFourPOS.service.SaleReturnItemService;
import com.scau.XFourPOS.service.SaleReturnService;

@Controller
@ParentPackage("struts-default")
public class SaleReturnAction extends BaseAction {
	@Autowired
	SaleReturnService saleReturnService;
	@Autowired
	SaleOrderService slaeOrderService;
	@Autowired
	SaleOrderItemService slaeOrderItemService;
	@Autowired
	SaleReturnItemService saleReturnItemService;
	@Autowired
	ProductService productService;
	
	private String itemId;
	private String productId;
	private String saleNO;
	private String returnNO;
	private String code;
	private String name;
	private double price;
	private int quanity;
	private double total;
	private String reason;
	@Action("a")//test
	public void a(){
		System.out.println("0hello\n");
		SaleOrderItem item=slaeOrderItemService.loadEntityByKey("id", "22");
		System.out.println("1hello\n"+item.toString());
		item.setQuanity(0);
		System.out.println("2hello\n");
		item.setPrice(0);
		System.out.println("3hello\n");
		slaeOrderItemService.updateEntity(item);
		System.out.println("4hello\n");
		System.out.println("bb============");
		
	}
	/**
	 * 退货处理
	 */
	@Action("saleReturnAction")
	public void saleReturn() {
		Json result = new Json();
		try {
		//更新订单saleOrder
		SaleOrder saleOrder=slaeOrderService.loadEntityByKey("saleOrderNO", saleNO);
		saleOrder.setTotal(saleOrder.getTotal()-total);
		slaeOrderService.updateEntity(saleOrder);
		//更新saleOrderItem
		SaleOrderItem item=slaeOrderItemService.loadEntityByKey("id",itemId);
		item.setQuanity(0);
		item.setPrice(0);
		slaeOrderItemService.updateEntity(item);
		//退单saleReturn
		SaleReturn saleReturn=saleReturnService.loadEntityByKey("saleReturnNO", returnNO);
		saleReturn.setSaleOrder(saleOrder);
		saleReturn.setTotal(saleReturn.getTotal()+total);
		saleReturnService.updateEntity(saleReturn);
		//创建新的saleReturnItem
		SaleReturnItem saleReturnItem=new SaleReturnItem();
		saleReturnItem.setPrice(price);
		Product product=productService.loadEntityByKey("productId",productId);
		saleReturnItem.setProduct_id(product);
		saleReturnItem.setQuantity(quanity);
		saleReturnItem.setSaleReturn(saleReturn);
		saleReturnItem.setTotal(total);
		saleReturnItem.setReason(reason);
		saleReturnItemService.saveEntity(saleReturnItem);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMsg("退货失败");
		}
		//返回josn串
		result.setSuccess(true);
		result.setMsg("退货成功");
		writeJson(result);
	}
	@Action("saleReturnCreateAction")
	public void saleReturnCreate() { 
		Json result = new Json();
		SaleReturn saleReturn=new SaleReturn();
		saleReturnService.saveEntity(saleReturn);
		String saleReturnNO=saleReturn.getSaleReturnNO();
		result.setSuccess(true);
		result.setMsg(saleReturnNO);
		writeJson(result);
	}
	
	public String getItemId() {
		return itemId;
	}
	public void setItemId(String itemId) {
		this.itemId = itemId;
	}
	public String getProductId() {
		return productId;
	}
	public void setProductId(String productId) {
		this.productId = productId;
	}
	public String getSaleNO() {
		return saleNO;
	}
	public void setSaleNO(String saleNO) {
		this.saleNO = saleNO;
	}
	public String getReturnNO() {
		return returnNO;
	}
	public void setReturnNO(String returnNO) {
		this.returnNO = returnNO;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public int getQuanity() {
		return quanity;
	}
	public void setQuanity(int quanity) {
		this.quanity = quanity;
	}
	public double getTotal() {
		return total;
	}
	public void setTotal(double total) {
		this.total = total;
	}
	public String getReason() {
		return reason;
	}
	public void setReason(String reason) {
		this.reason = reason;
	}
	
	
}
